<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class File extends CI_Controller {
	
	function __construct()
	{
	    parent::__construct();
	    $this->load->helper(array('form', 'url'));
            $this->load->model('upfiles_model');
	}

	public function uploadpicture()
	{	
		$uploadpath = BASEPATH . '../data/upfiles/image/'.date('Ymd');
		//$uploadpath = './';
		$makepath = is_dir($uploadpath) or (mkdir($uploadpath, 0777));
		
		$config['upload_path'] = $uploadpath;
		$config['allowed_types'] = '*';
		$config['max_size'] = '1000';
		$config['max_width']  = '3600';
		$config['max_height']  = '2000';
		$config['file_name']  = mktime().''.rand(100,999);
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if ( ! $this->upload->do_upload('picture')){
		    $error = $this->upload->display_errors();
		    $data['status'] = 0;
			//$data['info'] = '上传错误'.json_encode($_POST).$uploadpath.$error;
			$data['info'] = '上传错误'.$error;
			echo json_encode($data);exit();
		   
		 }else{
		    $error = $this->upload->data();
		    $data = array();
			$data['status'] = 1;
			$data['id'] = '/data/upfiles/image/'.date('Ymd').'/'.$error['file_name'];
			$data['url'] =  '/data/upfiles/image/'.date('Ymd').'/'.$error['file_name'];
			$data['path'] = $error['full_path'];
			$data['info'] = $error;
			
			
			echo json_encode($data);exit();
		}
		
		
		
	}
	
	public function uploadpictures()
	{	
		$uploadpath = BASEPATH . '../data/upfiles/image/'.date('Ymd');
		//$uploadpath = './';
		$makepath = is_dir($uploadpath) or (mkdir($uploadpath, 0777));
		
		$config['upload_path'] = $uploadpath;
		$config['allowed_types'] = '*';
		$config['max_size'] = '1000';
		$config['max_width']  = '3600';
		$config['max_height']  = '2000';
		$config['file_name']  = mktime().''.rand(100,999);
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if ( ! $this->upload->do_upload('picture')){
		    $error = $this->upload->display_errors();
		    $data['status'] = 0;
			//$data['info'] = '上传错误'.json_encode($_POST).$uploadpath.$error;
			$data['info'] = '上传错误'.$error;
			echo json_encode($data);exit();
		   
		 }else{
		    $error = $this->upload->data();
		    $data = array();
			$data['status'] = 1;
			$data['id'] = '/data/upfiles/image/'.date('Ymd').'/'.$error['file_name'];
			$data['url'] =  '/data/upfiles/image/'.date('Ymd').'/'.$error['file_name'];
			$data['path'] = $error['full_path'];
			$data['info'] = $error;
			
			$data_file = array();
			$data_file['name'] = $error['client_name'];
			$data_file['bid'] = $_GET['bid'];
			$data_file['code'] = $_GET['file'];
			$data_file['type'] = 'pic';
			$data_file['contents'] = $error['client_name'];
			$data_file['filetype'] = $error['file_ext'];
			$data_file['filename'] = $error['file_name'];
			$data_file['filesize'] = $error['file_size'];
			$data_file['filesize'] = $error['file_size'];
			$data_file['fileurl'] = $data['url'];
			$data_file['addtime'] = time();
			$data_file['addip'] = $this->input->ip_address();
			$this->upfiles_model->insert($data_file);
			
			echo json_encode($data);exit();
		}
		
		
		
	}
        
        public function uploadpartner()
	{	
		$uploadpath = BASEPATH . '../data/upfiles/partner/'.date('Ymd');
		//$uploadpath = './';
		$makepath = is_dir($uploadpath) or (mkdir($uploadpath, 0777));
		
		$config['upload_path'] = $uploadpath;
		$config['allowed_types'] = '*';
		$config['max_size'] = '1000';
		$config['max_width']  = '3600';
		$config['max_height']  = '2000';
		$config['file_name']  = mktime().''.rand(100,999);
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if ( ! $this->upload->do_upload('picture')){
		    $error = $this->upload->display_errors();
		    $data['status'] = 0;
			//$data['info'] = '上传错误'.json_encode($_POST).$uploadpath.$error;
			$data['info'] = '上传错误'.$error;
			echo json_encode($data);exit();
		   
		 }else{
		    $error = $this->upload->data();
		    $data = array();
			$data['status'] = 1;
			$data['id'] = '/data/upfiles/partner/'.date('Ymd').'/'.$error['file_name'];
			$data['url'] =  '/data/upfiles/partner/'.date('Ymd').'/'.$error['file_name'];
			$data['path'] = $error['full_path'];
			$data['info'] = $error;
			
			
			echo json_encode($data);exit();
		}
		
		
		
	}
	//上传excel
        public function uploadxls(){
            $uploadpath = BASEPATH . '../data/upfiles/excel/'.date('Ymd');
            //$uploadpath = './';
            $makepath = is_dir($uploadpath) or (mkdir($uploadpath, 0777));
            $config['upload_path'] = $uploadpath;
            $config['allowed_types'] = '*';
            $config['max_size'] = '6000';
            $config['max_width']  = '3600';
            $config['max_height']  = '2000';
            $config['file_name']  = mktime().''.rand(100,999);
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if ( ! $this->upload->do_upload('excel')){
                $error = $this->upload->display_errors();
                $data['status'] = 0;
                $data['info'] = '上传错误'.$error;
                echo json_encode($data);exit();
             }else{
                $error = $this->upload->data();
                $data = array();
                $data['status'] = 1;
                $data['id'] = '/data/upfiles/excel/'.date('Ymd').'/'.$error['file_name'];
                $data['url'] =  '/data/upfiles/excel/'.date('Ymd').'/'.$error['file_name'];
                $data['path'] = $error['full_path'];
                $data['info'] = $error;
                
                $data_file = array();
                $data_file['name'] = $error['client_name'];
                $data_file['bid'] = '';
                $data_file['code'] = '';
                $data_file['type'] = 'xls';
                $data_file['contents'] = $error['client_name'];
                $data_file['filetype'] = $error['file_ext'];
                $data_file['filename'] = $error['file_name'];
                $data_file['filesize'] = $error['file_size'];
                $data_file['fileurl'] = $data['url'];
                $data_file['addtime'] = time();
                $data_file['addip'] = $this->input->ip_address();
                $this->upfiles_model->insert($data_file);
                
                echo json_encode($data);exit();
            }
        }
	public function uploadbanner()
	{	
		$uploadpath = BASEPATH . '../data/upfiles/banner/'.date('Ymd');
		//$uploadpath = './';
		$makepath = is_dir($uploadpath) or (mkdir($uploadpath, 0777));
		
		$config['upload_path'] = $uploadpath;
		$config['allowed_types'] = '*';
		$config['max_size'] = '1000';
		$config['max_width']  = '3600';
		$config['max_height']  = '2000';
		$config['file_name']  = mktime().''.rand(100,999);
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		if ( ! $this->upload->do_upload('picture')){
		    $error = $this->upload->display_errors();
		    $data['status'] = 0;
			//$data['info'] = '上传错误'.json_encode($_POST).$uploadpath.$error;
			$data['info'] = '上传错误'.$error;
			echo json_encode($data);exit();
		   
		 }else{
		    $error = $this->upload->data();
		    $data = array();
			$data['status'] = 1;
			$data['id'] = '/data/upfiles/banner/'.date('Ymd').'/'.$error['file_name'];
			$data['url'] =  '/data/upfiles/banner/'.date('Ymd').'/'.$error['file_name'];
			$data['path'] = $error['full_path'];
			$data['info'] = $error;
			
			
			echo json_encode($data);exit();
		}
		
		
		
	}
	
}
